//
//  ViewController.m
//  appmanager
//
//  Created by 楼楚 on 15/10/14.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic ,strong) NSArray *appList;


@end

@implementation ViewController

- (NSArray *)appList{
    if (_appList == nil) {
        _appList = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]];
    }
    return _appList;
}

/*
 without storyboard 
 all code
 九宫格
 */
- (void)viewDidLoad {
    [super viewDidLoad];

//    搭建界面 九宫格
#define kAppViewW 80
#define kAppViewH 90
#define kColCount 3
#define kStartY 20
    CGFloat marginX = (self.view.bounds.size.width - kColCount *kAppViewW) / (kColCount +1);
    CGFloat marginY = 10;
    
    
    for (int i =0; i <12; i++) {
//        row
        int row = i/kColCount;
//     column
        int col = i % kColCount;
        
        CGFloat x = marginX + col *(marginX+kAppViewW);
        CGFloat y = kStartY+marginY + row *(marginY +kAppViewH);
        
    UIView *appView = [[UIView alloc]initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)];
//    appView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:appView];
//        实现视图内部细节
        NSDictionary *dict = self.appList[i];
//        1.UIImageView
        UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kAppViewW, 50)];
//        icon.backgroundColor = [UIColor greenColor];
        //        设置图像
        icon.image = [UIImage imageNamed:dict[@"icon"]];
//        设置图像填充模式 等比例显示
        icon.contentMode = UIViewContentModeScaleAspectFit;
        
        [appView addSubview:icon];

        
//        2.UILabel －>应用程序名称
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(icon.frame), kAppViewW, 20)];
//        label.backgroundColor = [UIColor redColor];
        //        设置应用程序名称
        label.text = dict[@"name"];
//        设置字体
        label.font = [UIFont systemFontOfSize:13.0];
        label.textAlignment= NSTextAlignmentCenter;
        [appView addSubview:label];

//        3.UIButton －>下载按钮
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame), kAppViewW, 20)];
//        button.backgroundColor = [UIColor grayColor];
//        背景图片
        [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
        
    [button setTitle:@"下载" forState:UIControlStateNormal];
//        修改字体
        button.titleLabel.font = [UIFont systemFontOfSize:13.0];
        
        [appView addSubview:button];
    }
   

}

@end
